package com.thefriststage.module4.homework3;

import java.util.concurrent.locks.ReentrantLock;

/*
火车买票案例 (两种实现方式) :
共100张票,三个线程一起卖票,
不允许出现出现负票和多卖的现象  (描述设置锁的时候要注意的事项:(Thread 和Runnable)

作者：翁海龙
*/
public class TrainTicket2  extends Thread  {
    private static int ticket = 100;
    //private static Object obj = new Object();		//如果用引用数据类型成员变量当作锁对象,必须是静态的
    public void run() {
        while(true) {
            synchronized(TrainTicket2.class) {
                if(ticket <= 0) {
                    break;
                }
                try {
                    Thread.sleep(10);				//线程1睡,线程2睡,线程3睡,线程4睡
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }
                System.out.println(getName() + "...这是第" + ticket-- + "号票");
            }
        }
    }



    public static void main(String[] args)  {

        TrainTicket2 tt=new TrainTicket2();

        Thread t1=new Thread(tt,"窗口一");
        Thread t2=new Thread(tt,"窗口二");
        Thread t3=new Thread(tt,"窗口三");

        t1.start();
        t2.start();
        t3.start();

    }
}
